package com.hxzy.goods.service.impl;

import com.hxzy.goods.mapper.BrandMapper;
import com.hxzy.goods.pojo.Brand;
import com.hxzy.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;



    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }

    public Brand findById(Integer id) {
        System.out.println("查询的编号为：" + id);
        return brandMapper.selectByPrimaryKey(id);
    }

    public int updateBrand(Brand brand) {
        return brandMapper.updateByPrimaryKeySelective(brand);
    }

    public int deleteById(Integer id) {
        return brandMapper.deleteById(id);
    }

    public int addBrand(Brand brand) {
//        return brandMapper.insert(brand);
          //当对象中数据为空时，该数据不插入
          return brandMapper.insertSelective(brand);
    }

    public List<Brand> findBrandBySelector(Map<String, Object> searchMap) {
        Example example=new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 品牌名称
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            // 品牌的首字母
            if(searchMap.get("letter")!=null && !"".equals(searchMap.get("letter"))){
                criteria.andEqualTo("letter",searchMap.get("letter"));
            }
        }
        return brandMapper.selectByExample(example);
    }

    public List<Map> findListByCategoryName(String categoryName) {
        return brandMapper.findListByCategoryName(categoryName);
    }

    public List<Map> findListSpecByCategoryName(String categoryName) {
        List<Map> specList = brandMapper.findSpecListByCategoryName(categoryName);
        for(Map spec:specList){
            String[] options = ((String) spec.get("options")).split(",");//规格选项列表
            spec.put("options",options);
        }
        return specList;
    }
}
